import router from "../router";
import store from "../store";
import { getStore } from "@/plugins/localStore";
import { default as api } from "./api"; // 设置浏览器头部标题

import manageLeftMenuData from "@/components/left_menu/leftMenuData";

const whiteList = [
	"/",
	"/login",
	"/specialLogin",
	"/szzt.html",
	"/scrw.html",
	"/scdd.html",
	"/ccrw.html",
	"/yyzx.html",
	"/ybfb.html",
	"/ubao.html",
	"/shenbao.html"
]; // 不重定向白名单
router.beforeEach((to, from, next) => {
	let allPermissionNarHead = {};
	let path = to.path;
	if (whiteList.indexOf(path) > -1) {
		next();
	} else {
		let user = getStore();
		console.log("------------store--------");
		console.log(user);
		if (user) {
			for (let k in manageLeftMenuData) {
				let router = manageLeftMenuData[k];
				if (router.meta && router.meta.permission) {
					allPermissionNarHead[router.path] = router.meta.permission;
				}
				if (router.children) {
					for (let child of router.children) {
						let r = router.path + "/" + child.path;
						if (child.meta && child.meta.permission) {
							allPermissionNarHead[r] = child.meta.permission;
						}
					}
				}
			}

			// setTimeout(()=>{
			// 	store.dispatch('user/setPermission',_permission||[]);
			// 	store.dispatch('permission/generateRoutes').then(() => { // 根据roles权限生成可访问的路由表
			// 		router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
			// 		next({...to});
			// 	})
			// },100)

			let url = "/jinl/role/queryRolePermission";
			api({
				url: url,
				method: "post",
				params: { roleId: user.roleId, notToken: true }
			})
				.then(function(response) {
					let _permission = response.result;
					for (let r of _permission) {
						if (r.split(":").length > 2) {
							let p_r = r.substring(0, r.lastIndexOf(":"));
							if (_permission.indexOf(p_r) < 0) {
								_permission.push(p_r);
							}
						}
					}
					store.dispatch("user/setPermission", _permission || []);
					// 如果是特殊用户，生成特殊的路由表
					if (user.flag) {
						store.dispatch("permission/getSpecialRouter")
							.then(() => {
								// 根据roles权限生成可访问的路由表
								router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
								console.log(store.getters.addRouters);
								let path = to.path;
								let access = allPermissionNarHead[path];
								if (
									access &&
									_permission.indexOf(access) < 0 &&
									user.type != 1
								) {
									next({ path: "/specialLogin" });
								} else {
									next({ ...to });
								}
							});
					} else {
						store.dispatch("permission/generateRoutes").then(() => {
							// 根据roles权限生成可访问的路由表
							router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
							console.log(store.getters.addRouters);
							let path = to.path;
							let access = allPermissionNarHead[path];
							if (
								access &&
								_permission.indexOf(access) < 0 &&
								user.type != 1
							) {
								next({ path: "/login" });
							} else {
								next({ ...to });
							}
						});
					}
				})
				.catch(function(resp) {
					next({ path: "/login" });
				});
		} else {
			next({ path: "/login" });
		}
	}
	next();
});

router.afterEach(() => {
	setTimeout(() => {
		window.document.title = "金利塑业5G数字化运营管理云平台方案";
		// window.document.title = "雨鑫木业5G数字化运营管理云平台"
	}, 0);
});
